package cn.tedu.boot52.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class PicController {
    @RequestMapping("/upload")
    public void upload(String intro, MultipartFile picFile) throws IOException {
        System.out.println("intro = " + intro + ", picFile = " + picFile);
        //得到上传文件的原始文件名
        String fileName = picFile.getOriginalFilename();
        //得到文件后缀    a.jpg
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        //给文件起一个唯一的名字 UUID.randomUUID()得到一个16进制的唯一标识字符串
        fileName = UUID.randomUUID()+suffix;
        String dirpath = "D:/upload/";
        File dirFile = new File(dirpath);
        if (!dirFile.exists()){
            dirFile.mkdirs();
        }
        //得到文件的完整路径
        String filePath = dirpath+fileName;
        //把文件保存到上面的路径  异常抛出
        picFile.transferTo(new File(filePath));
    }
}
